String.prototype.PadLeft = function(totalWidth, paddingChar)
{
    if ( paddingChar != null )
    {
    return this.PadHelper(totalWidth, paddingChar, false);
    } else {
    return this.PadHelper(totalWidth, ' ', false);
    }
}
String.prototype.PadRight = function(totalWidth, paddingChar)
{
    if ( paddingChar != null )
    {
    return this.PadHelper(totalWidth, paddingChar, true);
    } else {
    return this.PadHelper(totalWidth, ' ', true);
    }
}
String.prototype.PadHelper = function(totalWidth, paddingChar, isRightPadded)
{
    if ( this.length < totalWidth)
    {
    var paddingString = new String();
    for (i = 1; i <= (totalWidth - this.length); i++)
    {
    paddingString += paddingChar;
    }
    if ( isRightPadded )
    {
    return (this + paddingString);
    } else {
    return (paddingString + this);
    }
    } else {
    return this;
    }
}
var common = {
    get_data:function(key){
        return window.localStorage.getItem(key);
    },
    set_data:function(key,value){
        window.localStorage.setItem(key,value);
    },
    get_json_data:function(key){
        return JSON.parse(window.localStorage.getItem(key));
    },
    set_json_data:function(key,value){
        window.localStorage.setItem(key,JSON.stringify(value));
    },
    //页面间的参数提取、设置方法
    get_page_data:function(key){
        return window.sessionStorage.getItem(key);
    },
    //页面间的参数提取、设置方法
    set_page_data:function(key,value){
        window.sessionStorage.setItem(key,value);
    },
    //页面间的参数提取、设置方法
    get_page_json_data:function(key){
        return JSON.parse(window.sessionStorage.getItem(key));
    },
    //页面间的参数提取、设置方法
    set_page_json_data:function(key,value){
        window.sessionStorage.setItem(key,JSON.stringify(value));
    },
    //获得完整的url访问路径
    get_url:function(url){
        return this.get_data("base_url")+"/"+this.get_data("appname")+url;
    },
    //获得完整的url访问路径
    get_imgurl:function(url){
        return this.get_data("base_url")+"/"+this.get_data("appname")+"/"+url;
    },
    //获得头像地址
    get_avatar:function(url){
        if(url == null || url == ""){
            return "../img/Avatar-Default.png";
        }
        else{
            return this.get_imgurl(url);
        }
    },
    //获得sessionID
    get_sessionid:function(){
        return this.get_data("sessionID");
    },
    //设置sessionID
    set_sessionid:function(sessionID){
        this.set_data("sessionID",sessionID);
    },
    //检测是否登陆情况
    check_islogon:function(callback){
        var sessionID = this.get_sessionid();
        var result;
        if(sessionID == "-1"){
            //未登陆
            result = false;
        }
        else{
            //已登陆
            result = true;
        }
        if(callback){
            callback(result);
        }
        else{
            if(!result){
                navigator.notification.alert("尚未登录，先登录吧！",function(){
                    //window.location.replace("../User/Login.html");
                    window.location.href = "../User/Login.html";
                },"需要登录");
            }
        }
        return result;
    },
    //处理ajax请求错误情况
    deal_ajax_error:function(jqXHR) {
        if(jqXHR.status == "400"){
            //400错误，判断是否3020
            //alert(jqXHR.responseText);
            var errorobj = eval("("+jqXHR.responseText+")");
            if(errorobj.errorCode == "3010" || errorobj.errorCode == "3020"){
                //未登录
                var alertmsg = "";
                if(errorobj.errorCode == "3010"){
                    alertmsg = "尚未登录";
                }
                else{
                    alertmsg = "登录授权已经过期";
                }
                navigator.notification.alert(alertmsg,function(){
                    window.location.replace("../User/Login.html");
                },"需要登录");
            }
            else{
                navigator.notification.alert(errorobj.errorMessage,function(){
                    
                },"错误");
            }
        }
        else{
            //直接输出错误
            //alert(jqXHR.responseText);
        }
    },
    //获得图片文件的base64
    get_file_base64_data:function(file,callback){
        var base64_data;
        var reader = new FileReader();
        reader.error = function(fileerror){
            console.log("error is" + fileerror);
        };
        reader.onloadend = function (evt) {
            console.log("read success");
            //console.log(evt.target.result);
            base64_data = $.base64.btoa(evt.target.result);
            //$("#data2").html('<strong>Read as data text:</strong> <br><pre>' + evt.target.result + '</pre>');
            if(callback){
                callback(base64_data);
            }
        };
        reader.readAsBinaryString(file);
    },
    get_base64_data:function(path,callback){
        var me = this;
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
            function(fileSystem){
                window.resolveLocalFileSystemURI(path, 
                    function(fileEntry){
                        fileEntry.file(
                            function(file){
                                me.get_file_base64_data(file,callback);
                            }, 
                            function(error){
                                alert(error.code);
                            }
                        );
                    }, 
                    function(error){
                        alert(error.code);
                    }
                );
            }, 
            function(error){
                alert(error.code);
            }
        );
    },
    //生成缩略图图片
    get_resize_image:function(image_path,callback){
        if(!window.imageResizer){
            return;
        }
        window.imageResizer.resizeImage(
          function(data) { 
            //var image = document.getElementById('myImage');
            //image.src = "data:image/jpeg;base64," + data.imageData; 
            if(callback){
                callback(data.imageData);
            }
          }, 
          function (error) {
            console.log("Resize Image Error : \r\n" + error);
          }, 
          image_path, 
          640, 640, 
          {
            format:"jpg",
            imageDataType:ImageResizer.IMAGE_DATA_TYPE_URL,
            pixelDensity:true,
            storeImage:false
          }
        );
    },
    //获取Article图片
    get_article_img:function(success_callback,error_callback,config){
        navigator.camera.getPicture(success_callback, error_callback,{ 
          quality: 70,
          destinationType: Camera.DestinationType.DATA_URL,
          //PHOTOLIBRARY
          sourceType:Camera.PictureSourceType.Camera,
          allowEdit:true,
          targetWidth:640,
          targetHeight:640
        });
    },
    format_date:function (date, format) {
        if (arguments.length < 2 && !date.getTime) {
            format = date;
            date = new Date();
        }
        typeof format != 'string' && (format = 'YYYY年MM月DD日 hh时mm分ss秒');
        var week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', '日', '一', '二', '三', '四', '五', '六'];
        return format.replace(/YYYY|YY|MM|DD|hh|mm|ss|星期|周|www|week/g, function(a) {
            switch (a) {
            case "YYYY": return date.getFullYear();
            case "YY": return (date.getFullYear()+"").slice(2);
            case "MM": return (date.getMonth() + 1).toString().PadLeft(2,"0");
            case "DD": return date.getDate().toString().PadLeft(2,"0");
            case "hh": return date.getHours().toString().PadLeft(2,"0");
            case "mm": return date.getMinutes().toString().PadLeft(2,"0");
            case "ss": return date.getSeconds().toString().PadLeft(2,"0");
            case "星期": return "星期" + week[date.getDay() + 7];
            case "周": return "周" +  week[date.getDay() + 7];
            case "week": return week[date.getDay()];
            case "www": return week[date.getDay()].slice(0,3);
            }
        });
    },
    get_date_fromdb:function(str_date){
        var result = new Date(); 
        result.setTime(Date.parse(str_date));
        return result;
    },
    //获得到分钟的日期描述
    get_minute_date_desc:function(str_date){
        var result = this.get_date_fromdb(str_date);
        return this.format_date(result,"YYYY-MM-DD hh:mm");
    },
    //获得正常的日期描述
    get_normal_date_desc:function(str_date){
        var result = this.get_date_fromdb(str_date);
        return this.format_date(result,"YYYY-MM-DD");
    },
    //获得过去时间的描述
    get_passed_time_desc:function(str_date){
        var desc = "";
        if(!str_date){
            return desc;
        }
        var time_now,time_target,time_distance;
        var int_year,int_month,int_day,int_hour,int_minute,int_second;
        time_now = new Date();
        time_now = time_now.getTime();
        time_target = Date.parse(str_date);
        //time_target = time_target.getTime();
        time_distance = time_now - time_target;
        if(time_distance > 0)
        {
            int_year = Math.floor(time_distance/(86400000*12*30));
            time_distance -= int_year*86400000*12*30;
            int_month = Math.floor(time_distance/(86400000*30));
            time_distance -= int_month*86400000*30;
            int_day = Math.floor(time_distance/86400000);
            time_distance -= int_day*86400000;
            int_hour = Math.floor(time_distance/3600000);
            time_distance -= int_hour*3600000;
            int_minute = Math.floor(time_distance/60000);
            time_distance -= int_minute*60000;
            int_second = Math.floor(time_distance/1000);

            if(int_year > 0){
                desc = int_year + "年前";
                return desc;
            }
            if(int_month > 0){
                desc = int_month + "个月前";
                return desc;
            }
            if(int_day > 0){
                desc = int_day + "天前";
                return desc;
            }
            if(int_hour > 0){
                desc = int_hour + "小时前";
                return desc;
            }
            if(int_minute > 0){
                desc = int_minute + "分钟前";
                return desc;
            }
            if(int_second > 0){
                desc = "刚刚";
                return desc;
            }
        }
        else if(time_distance == 0)
        {
            desc = "刚刚"
        }
        else{
            desc = "";
        }
        return desc;
    },
    //动态css样式
    dynamic_css:function(){
        var bodywidth = window.screen.availWidth-30;
        var style = document.createElement("style");
        document.head.appendChild(style);
        var sheet = style.sheet;
        //动态增加img_article的css属性
        sheet.insertRule(".div_article {width:"+bodywidth+"px;height:"+bodywidth+"px;background-size: "+bodywidth+"px "+bodywidth+"px;background-repeat: no-repeat; position:relative;}",0);
        sheet.insertRule(".img_article {width:"+bodywidth+"px;height:"+bodywidth+"px;}",1);
    }
};
window.common = common;
common.dynamic_css();